﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.Runtime;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Configuration;
using System.Xml;
using System.Collections;
using Pub.Lib;
using Pub.Tools;
using System.IO;
using Pub;


namespace RemotingService
{
    public partial class RemotingService1 : ServiceBase
    {
        public RemotingService1()
        {
            InitializeComponent();

        }

        private TcpChannel tcpChannel;

        private TcpServerChannel tcpServerChannel;

        string strPort = string.Empty;
        string strMess = string.Empty;

        protected override void OnStart(string[] args)
        {
            WriteLog(1,"","REMOTING服务启动");
            
            string FileDir = AppDomain.CurrentDomain.SetupInformation.ApplicationBase.ToString();
            string SysSetXml = System.IO.Path.Combine(FileDir, @"xml\SysSet.xml");

            string _UpdateSQL = XmlTools.Read(SysSetXml, "SysSet/UpdateSQL");
            PubVar.SysUpdateSQL = SysTools.ToBoolean(_UpdateSQL);

            try
            {
                PubVar.Domain = Helper.DBHelper.GetInf("domain", PubVar.ConnMain, DataAccessType.CS).Trim();
            }
            catch (Exception ex)
            {
                SysTools.WriteTxt(PubVar.LogFile, "读取domain出错:" + ex.Message.ToString());
            }
            string strSecure_Username = DESEncrypt.Decrypt(XmlTools.Read(SysSetXml, "SysSet/Remoting/Username"));
            strPort = Pub.Tools.XmlTools.Read(SysSetXml, "SysSet/ServerPort");
            PubVar.GetSYSDAT();

            if (strSecure_Username != string.Empty)
            {
                strMess = Pub.Lib.RemotingServer.SecureStart(ref tcpChannel, strPort);
                SysUser.FaxSendedEvent += new FaxEventHandler(OnFaxSended);
            }
            else
            {
                strMess = Pub.Lib.RemotingServer.Start(ref tcpServerChannel, strPort);
            }
            WriteLog(1, "", strMess);
            SysTools.WriteEventLog("Remoting Service", strMess);

        }

        public void OnFaxSended(string strpc, string strfax)
        {
            WriteLog(3, strpc,strfax);
        }

        private void WriteLog(int iType,string strpc,string strLog )
        {

            //string FileDir = AppDomain.CurrentDomain.SetupInformation.ApplicationBase.ToString();
            //string FileName = @FileDir + "log.xml";

            string FileName = Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase.ToString(), @"xml\log.xml");

            XmlDocument xmlDoc = new XmlDocument();
            if (!System.IO.File.Exists(@FileName))
            { return; }
            xmlDoc.Load(@FileName);

            DataTable dtt = XmlDB.XmlToDataTable(xmlDoc.OuterXml);
            DataRow dr = dtt.NewRow();
            dr["dDate"] = System.DateTime.Now;
            dr["iType"] = iType;
            dr["cIP"] = strpc;            
            dr["cText"] = strLog;

            dtt.Rows.Add(dr);

            xmlDoc.LoadXml(XmlDB.DataToXml(dtt));

            xmlDoc.Save(@FileName);
        }

        protected override void OnStop()
        {
            WriteLog(1, "", "REMOTING服务结束");         
        }
    }
}
